// var i;
// var $item_Shop_GoldItem_UIBindings = require("./Item_Shop_GoldItem_UIBindings");
// var $resKeeper = require("./ResKeeper");
// var $excel = require("./Excel");
// var $hallManager = require("./HallManager");
// var $playerDataManager = require("./PlayerDataManager");
// var $tipsManager = require("./TipsManager");
// var $const_Hall = require("./Const_Hall");
// var $uIManager = require("./UIManager");
// var $bundleConfig = require("./BundleConfig");
// var $uIConfig_Home = require("./UIConfig_Home");
// var $aDManager = require("./ADManager");
// var $eventManager = require("./EventManager");
// var $evenType = require("./EvenType");
// var $redPointControl = require("./RedPointControl");
// var $hallGlobalVariable = require("./HallGlobalVariable");
// var $commonUtils = require("./CommonUtils");
// var $const_Common = require("./Const_Common");
// var E = cc._decorator;
// var x = E.ccclass;
// var C = E.property;
// var B = E.inspector;
// var k = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.auto_ui = null;
//         t.isStart = !0;
//         t.childIndex = 0;
//         t.addCount = 0;
//         t.priceNum = 0;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.StartSet = function (e) {
//         var t = this.node.getChildByName("redPoint");
//         if (t) {
//             t.active = 0 == e;
//         }
//         var o = $excel.Excel.shopControl(1);
//         if (this.isStart) {
//             this.isStart = !1;
//             this.childIndex = e;
//             switch (e) {
//                 case 0:
//                     this.auto_ui.txt_nameLabel.string = "一点金币";
//                     this.auto_ui.txt_countLabel.string = "X" + o.gold1Count;
//                     this.addCount = o.gold1Count;
//                     break;
//                 case 1:
//                     this.auto_ui.txt_nameLabel.string = "一袋金币";
//                     this.auto_ui.txt_countLabel.string = "X" + o.gold2Count;
//                     this.auto_ui.txt_priceLabel.string = "" + o.gold2Price;
//                     this.auto_ui.node_diamondIcon.active = !0;
//                     this.addCount = o.gold2Count;
//                     this.priceNum = o.gold2Price;
//                     break;
//                 case 2:
//                     this.auto_ui.txt_nameLabel.string = "一箱金币";
//                     this.auto_ui.txt_countLabel.string = "X" + o.gold3Count;
//                     this.auto_ui.txt_priceLabel.string = "" + o.gold3Price;
//                     this.auto_ui.node_diamondIcon.active = !0;
//                     this.addCount = o.gold3Count;
//                     this.priceNum = o.gold3Price;
//             }
//         }
//         if (0 == e) {
//             this.auto_ui.btn_buyBtn.interactable = !0;
//             if (0 == $hallManager.HallMgr.shopData.videoBuyGoldCount) {
//                 this.auto_ui.txt_priceLabel.string = "免费";
//                 this.auto_ui.node_videoIcon.active = !1;
//             } else {
//                 {
//                     var n = o.gold1Time - $hallManager.HallMgr.shopData.videoBuyGoldCount + 1;
//                     if (n <= 0) {
//                         this.auto_ui.txt_priceLabel.string = "已售罄";
//                         this.auto_ui.node_videoIcon.active = !1;
//                         this.auto_ui.btn_buyBtn.interactable = !1;
//                     } else {
//                         this.auto_ui.txt_priceLabel.string = "免费(" + n + ")";
//                         this.auto_ui.node_videoIcon.active = !0;
//                     }
//                 }
//             }
//         }
//     };
//     t.prototype.onLoad = function () {
//         this.auto_ui.addClickListener(this._clickListener, this);
//     };
//     t.prototype._clickListener = function (e) {
//         switch (e) {
//             case this.auto_ui.btn_buyBtn:
//                 this.BuyThis();
//         }
//     };
//     t.prototype.BuyThis = function () {
//         var e = this;
//         if (0 == this.childIndex) {
//             if (0 == $excel.Excel.shopControl(1).gold1Time - $hallManager.HallMgr.shopData.videoBuyGoldCount + 1) {
//                 return void $tipsManager.TipMgr.showToast("已售罄");
//             }
//             if (0 == $hallManager.HallMgr.shopData.videoBuyGoldCount) {
//                 this.VideoBuyCount();
//             } else {
//                 $aDManager.ADMgr.showRewardedVideo("商城视频购买金币", function () {
//                     e.VideoBuyCount();
//                 });
//             }
//         } else {
//             var t = this;
//             var o = new $const_Hall.ShopItemInfoData();
//             o.iconSpriteFrame = this.auto_ui.sp_iconSprite.spriteFrame;
//             o.qualitySpriteFrame = null;
//             o.titleName = this.auto_ui.txt_nameLabel.string;
//             o.count = this.addCount;
//             o.richType = $const_Hall.RichType.钻石;
//             o.price = this.priceNum;
//             o.infoStr = "金币可用于装备升级，可通过主线关卡获得";
//             o.clickCallBack = function () {
//                 if ($playerDataManager.PlayerDataMgr.SubDiamond(t.priceNum)) {
//                     $commonUtils.default.SendPoint(
//                         $const_Common.SendPointType.外围打点,
//                         "钻石购买" + t.addCount + "金币"
//                     );
//                     t.ShowGetReward(t.addCount, function () {
//                         $playerDataManager.PlayerDataMgr.AddGold(t.addCount);
//                         $tipsManager.TipMgr.showToast("购买成功，获得金币" + t.addCount);
//                     });
//                 }
//             };
//             $uIManager.default.open($bundleConfig.BundleNames.Home, $uIConfig_Home.UIView_Home.UI_ShopItemInfo, o);
//         }
//     };
//     t.prototype.VideoBuyCount = function () {
//         var e = this;
//         this.ShowGetReward(e.addCount, function () {
//             $playerDataManager.PlayerDataMgr.AddGold(e.addCount);
//             $tipsManager.TipMgr.showToast("购买成功，获得金币" + e.addCount);
//             $hallManager.HallMgr.shopData.videoBuyGoldCount++;
//             if (1 == $hallManager.HallMgr.shopData.videoBuyGoldCount) {
//                 $eventManager.EventMgr.dispatchEvent(
//                     $evenType.EVENT_TYPE.MainRedPointChange,
//                     $redPointControl.RedPointType.freeGold
//                 );
//             }
//             $hallManager.HallMgr.SaveShopData();
//             e.StartSet(e.childIndex);
//         });
//     };
//     t.prototype.ShowGetReward = function (e, t) {
//         var o = new $const_Hall.GetItemsData();
//         o.ids = [$hallGlobalVariable.default.goldId];
//         o.counts = [e];
//         o.clickCallBack = t;
//         $uIManager.default.open($bundleConfig.BundleNames.Home, $uIConfig_Home.UIView_Home.UI_GetItems, o);
//     };
//     __decorate([C($item_Shop_GoldItem_UIBindings.default)], t.prototype, "auto_ui", void 0);
//     return __decorate([x], t);
// })($resKeeper.default);
// exports.default = k;

// import Item_Shop_GoldItem_UIBindings from "./Item_Shop_GoldItem_UIBindings";
// import ResKeeper from "./ResKeeper";
// import { Excel } from "./Excel";
// import { HallMgrInstance } from "./HallManager";
// import {PlayerDataMgr} from "./PlayerDataManager";
// import {TipMgrInstance} from "./TipsManager";
// import { GetItemsData, RichType, ShopItemInfoData } from "./Const_Hall";
// import UIManager from "./UIManager";
// import { BundleNames } from "./BundleConfig";
// import { UIView_Home } from "./UIConfig_Home";
// import {ADMgr} from "./ADManager";
// import {EventMgrInstance} from "./EventManager";
// import {EVENT_TYPE} from "./EvenType";
// import { RedPointCtrlInstance, RedPointType } from "./RedPointControl";
// import HallGlobalVariable from "./HallGlobalVariable";
// import CommonUtils from "./CommonUtils";
// import { SendPointType } from "./Const_Common";

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名：ShopGoldItem
//  */
// @ccclass
// class Item_Shop_GoldItem extends ResKeeper {
//     @property(Item_Shop_GoldItem_UIBindings)
//     auto_ui: any = null;

//     isStart: boolean = true;
//     childIndex: number = 0;
//     addCount: number = 0;
//     priceNum: number = 0;

//     /**
//      * 初始化设置
//      * @param index - 子项索引
//      * 建议方法名：initializeSettings
//      */
//     StartSet(index: number): void {
//         const redPointNode = this.node.getChildByName("redPoint");
//         if (redPointNode) {
//             redPointNode.active = index === 0;
//         }

//         const shopControlData = Excel.shopControl(1);

//         if (this.isStart) {
//             this.isStart = false;
//             this.childIndex = index;

//             switch (index) {
//                 case 0:
//                     this.auto_ui.txt_nameLabel.string = "一点金币";
//                     this.auto_ui.txt_countLabel.string = "X" + shopControlData.gold1Count;
//                     this.addCount = shopControlData.gold1Count;
//                     break;
//                 case 1:
//                     this.auto_ui.txt_nameLabel.string = "一袋金币";
//                     this.auto_ui.txt_countLabel.string = "X" + shopControlData.gold2Count;
//                     this.auto_ui.txt_priceLabel.string = "" + shopControlData.gold2Price;
//                     this.auto_ui.node_diamondIcon.active = true;
//                     this.addCount = shopControlData.gold2Count;
//                     this.priceNum = shopControlData.gold2Price;
//                     break;
//                 case 2:
//                     this.auto_ui.txt_nameLabel.string = "一箱金币";
//                     this.auto_ui.txt_countLabel.string = "X" + shopControlData.gold3Count;
//                     this.auto_ui.txt_priceLabel.string = "" + shopControlData.gold3Price;
//                     this.auto_ui.node_diamondIcon.active = true;
//                     this.addCount = shopControlData.gold3Count;
//                     this.priceNum = shopControlData.gold3Price;
//                     break;
//             }
//         }

//         if (index === 0) {
//             this.auto_ui.btn_buyBtn.interactable = true;
//             if (HallMgrInstance.shopData.videoBuyGoldCount === 0) {
//                 this.auto_ui.txt_priceLabel.string = "免费";
//                 this.auto_ui.node_videoIcon.active = false;
//             } else {
//                 const remainingTimes = shopControlData.gold1Time - HallMgrInstance.shopData.videoBuyGoldCount + 1;
//                 if (remainingTimes <= 0) {
//                     this.auto_ui.txt_priceLabel.string = "已售罄";
//                     this.auto_ui.node_videoIcon.active = false;
//                     this.auto_ui.btn_buyBtn.interactable = false;
//                 } else {
//                     this.auto_ui.txt_priceLabel.string = `免费(${remainingTimes})`;
//                     this.auto_ui.node_videoIcon.active = true;
//                 }
//             }
//         }
//     }

//     /**
//      * 加载时的初始化
//      * 建议方法名：initializeOnLoad
//      */
//     onLoad(): void {
//         this.auto_ui.addClickListener(this._clickListener, this);
//     }

//     /**
//      * 点击事件监听器
//      * @param event - 点击事件
//      * 建议方法名：handleClickEvent
//      */
//     private _clickListener(event: cc.Button): void {
//         if (event === this.auto_ui.btn_buyBtn) {
//             this.BuyThis();
//         }
//     }

//     /**
//      * 购买当前物品
//      * 建议方法名：purchaseItem
//      */
//     BuyThis(): void {
//         if (this.childIndex === 0) {
//             const remainingTimes = Excel.shopControl(1).gold1Time - HallMgrInstance.shopData.videoBuyGoldCount + 1;
//             if (remainingTimes <= 0) {
//                 TipMgrInstance.showToast("已售罄");
//                 return;
//             }

//             if (HallMgrInstance.shopData.videoBuyGoldCount === 0) {
//                 this.VideoBuyCount();
//             } else {
//                 ADMgr.showRewardedVideo("商城视频购买金币", () => {
//                     this.VideoBuyCount();
//                 });
//             }
//         } else {
//             const shopItemInfo = new ShopItemInfoData();
//             shopItemInfo.iconSpriteFrame = this.auto_ui.sp_iconSprite.spriteFrame;
//             shopItemInfo.qualitySpriteFrame = null;
//             shopItemInfo.titleName = this.auto_ui.txt_nameLabel.string;
//             shopItemInfo.count = this.addCount;
//             shopItemInfo.richType = RichType.钻石;
//             shopItemInfo.price = this.priceNum;
//             shopItemInfo.infoStr = "金币可用于装备升级，可通过主线关卡获得";
//             shopItemInfo.clickCallBack = () => {
//                 if (PlayerDataMgr.SubDiamond(this.priceNum)) {
//                     CommonUtils.SendPoint(SendPointType.外围打点, `钻石购买${this.addCount}金币`);
//                     this.ShowGetReward(this.addCount, () => {
//                         PlayerDataMgr.AddGold(this.addCount);
//                         TipMgrInstance.showToast(`购买成功，获得金币${this.addCount}`);
//                     });
//                 }
//             };
//             UIManager.open(BundleNames.Home, UIView_Home.UI_ShopItemInfo, shopItemInfo);
//         }
//     }

//     /**
//      * 视频购买金币计数
//      * 建议方法名：incrementVideoBuyCount
//      */
//     VideoBuyCount(): void {
//         this.ShowGetReward(this.addCount, () => {
//             PlayerDataMgr.AddGold(this.addCount);
//             TipMgrInstance.showToast(`购买成功，获得金币${this.addCount}`);
//             HallMgrInstance.shopData.videoBuyGoldCount++;
//             if (HallMgrInstance.shopData.videoBuyGoldCount === 1) {
//                 EventMgrInstance.dispatchEvent(EVENT_TYPE.MainRedPointChange, RedPointType.freeGold);
//             }
//             HallMgrInstance.SaveShopData();
//             this.StartSet(this.childIndex);
//         });
//     }

//     /**
//      * 显示获得奖励
//      * @param count - 奖励数量
//      * @param callback - 回调函数
//      * 建议方法名：displayReward
//      */
//     ShowGetReward(count: number, callback: Function): void {
//         const getItemsData = new GetItemsData();
//         getItemsData.ids = [HallGlobalVariable.goldId];
//         getItemsData.counts = [count];
//         getItemsData.clickCallBack = callback;
//         UIManager.open(BundleNames.Home, UIView_Home.UI_GetItems, getItemsData);
//     }
// }

// export default Item_Shop_GoldItem;

import ItemShopGoldItemUIBindings from "./Item_Shop_GoldItem_UIBindings";
import ResKeeper from "./ResKeeper";
import { Excel } from "./Excel";
import { HallMgr } from "./HallManager";
import { PlayerDataMgr } from "./PlayerDataManager";
import { TipMgr } from "./TipsManager";
import { GetItemsData, RichType, ShopItemInfoData } from "./Const_Hall";
import UIManager from "./UIManager";
import { BundleNames } from "./BundleConfig";
import { UIView_Home } from "./UIConfig_Home";
import { ADMgr } from "./ADManager";
import { EventMgrInstance } from "./EventManager";
import { EVENT_TYPE } from "./EvenType";
import { RedPointType } from "./RedPointControl";
import FruitDataManager from "./HallGlobalVariable";

const { ccclass, property, inspector } = cc._decorator;

/**
 * 建议类名：ShopGoldItem
 */
@ccclass
class ShopGoldItem extends ResKeeper {
    @property(ItemShopGoldItemUIBindings)
    auto_ui: ItemShopGoldItemUIBindings = null;

    isStart: boolean = true;
    childIndex: number = 0;
    addCount: number = 0;
    priceNum: number = 0;

    /**
     * 初始化设置
     * @param itemIndex - 子项索引
     * 建议方法名：initializeSettings
     */
    initializeSettings(itemIndex: number): void {
        const redPointNode = this.node.getChildByName("redPoint");
        if (redPointNode) {
            redPointNode.active = itemIndex === 0;
        }

        const shopControlData = Excel.shopControl(1);

        if (this.isStart) {
            this.isStart = false;
            this.childIndex = itemIndex;

            switch (itemIndex) {
                case 0:
                    this.auto_ui.txt_nameLabel.string = "一点金币";
                    this.auto_ui.txt_countLabel.string = `X${shopControlData.gold1Count}`;
                    this.addCount = shopControlData.gold1Count;
                    break;
                case 1:
                    this.auto_ui.txt_nameLabel.string = "一袋金币";
                    this.auto_ui.txt_countLabel.string = `X${shopControlData.gold2Count}`;
                    this.auto_ui.txt_priceLabel.string = `${shopControlData.gold2Price}`;
                    this.auto_ui.node_diamondIcon.active = true;
                    this.addCount = shopControlData.gold2Count;
                    this.priceNum = shopControlData.gold2Price;
                    break;
                case 2:
                    this.auto_ui.txt_nameLabel.string = "一箱金币";
                    this.auto_ui.txt_countLabel.string = `X${shopControlData.gold3Count}`;
                    this.auto_ui.txt_priceLabel.string = `${shopControlData.gold3Price}`;
                    this.auto_ui.node_diamondIcon.active = true;
                    this.addCount = shopControlData.gold3Count;
                    this.priceNum = shopControlData.gold3Price;
                    break;
            }
        }

        if (itemIndex === 0) {
            this.auto_ui.btn_buyBtn.interactable = true;
            if (HallMgr.shopData.videoBuyGoldCount === 0) {
                this.auto_ui.txt_priceLabel.string = "免费";
                this.auto_ui.node_videoIcon.active = false;
            } else {
                const remainingTimes = shopControlData.gold1Time - HallMgr.shopData.videoBuyGoldCount + 1;
                if (remainingTimes <= 0) {
                    this.auto_ui.txt_priceLabel.string = "已售罄";
                    this.auto_ui.node_videoIcon.active = false;
                    this.auto_ui.btn_buyBtn.interactable = false;
                } else {
                    this.auto_ui.txt_priceLabel.string = `免费(${remainingTimes})`;
                    this.auto_ui.node_videoIcon.active = true;
                }
            }
        }
    }

    /**
     * 加载时的初始化
     * 建议方法名：initializeOnLoad
     */
    onLoad(): void {
        this.auto_ui.addClickListener(this.handleClickEvent, this);
    }

    /**
     * 点击事件监听器
     * @param button - 点击事件按钮
     * 建议方法名：handleClickEvent
     */
    private handleClickEvent(button: cc.Button): void {
        if (button === this.auto_ui.btn_buyBtn) {
            this.purchaseItem();
        }
    }

    /**
     * 购买当前物品
     * 建议方法名：purchaseItem
     */
    purchaseItem(): void {
        if (this.childIndex === 0) {
            const remainingTimes = Excel.shopControl(1).gold1Time - HallMgr.shopData.videoBuyGoldCount + 1;
            if (remainingTimes <= 0) {
                TipMgr.showToast("已售罄");
                return;
            }

            if (HallMgr.shopData.videoBuyGoldCount === 0) {
                this.incrementVideoBuyCount();
            } else {
                ADMgr.showRewardedVideoAd("商城视频购买金币", () => {
                    this.incrementVideoBuyCount();
                });
            }
        } else {
            const shopItemInfo = new ShopItemInfoData();
            shopItemInfo.iconSpriteFrame = this.auto_ui.sp_iconSprite.spriteFrame;
            shopItemInfo.qualitySpriteFrame = null;
            shopItemInfo.titleName = this.auto_ui.txt_nameLabel.string;
            shopItemInfo.count = this.addCount;
            shopItemInfo.richType = RichType.Diamond;
            shopItemInfo.price = this.priceNum;
            shopItemInfo.infoStr = "金币可用于装备升级，可通过主线关卡获得";
            shopItemInfo.clickCallBack = () => {
                if (PlayerDataMgr.subtractDiamond(this.priceNum)) {
                    // CommonUtils.sendTrackingEvent(SendPointType.OuterAreaMarking, `钻石购买${this.addCount}金币`);
                    this.displayReward(this.addCount, () => {
                        PlayerDataMgr.addGold(this.addCount);
                        TipMgr.showToast(`购买成功，获得金币${this.addCount}`);
                    });
                }
            };
            UIManager.open(BundleNames.Home, UIView_Home.UI_ShopItemInfo, shopItemInfo);
        }
    }

    /**
     * 视频购买金币计数
     * 建议方法名：incrementVideoBuyCount
     */
    incrementVideoBuyCount(): void {
        this.displayReward(this.addCount, () => {
            PlayerDataMgr.addGold(this.addCount);
            TipMgr.showToast(`购买成功，获得金币${this.addCount}`);
            HallMgr.shopData.videoBuyGoldCount++;
            if (HallMgr.shopData.videoBuyGoldCount === 1) {
                EventMgrInstance.emitEvent(EVENT_TYPE.MainRedPointChange, RedPointType.freeGold);
            }
            HallMgr.saveShopData();
            this.initializeSettings(this.childIndex);
        });
    }

    /**
     * 显示获得奖励
     * @param rewardCount - 奖励数量
     * @param rewardCallback - 回调函数
     * 建议方法名：displayReward
     */
    displayReward(rewardCount: number, rewardCallback: Function): void {
        const getItemsData = new GetItemsData();
        getItemsData.ids = [FruitDataManager.goldId];
        getItemsData.counts = [rewardCount];
        getItemsData.clickCallBack = rewardCallback;
        UIManager.open(BundleNames.Home, UIView_Home.UI_GetItems, getItemsData);
    }
}

export default ShopGoldItem;